﻿using BlueWePush.Service.WxModel;

namespace BlueWePush.Service.BaseService
{
    public class BlueWePushService
    {
        /// <summary>
        /// 获取access_token:有效期固定2小时,频繁请求token会被官方拦截屏蔽
        /// </summary>
        /// <param name="corpid">企业id</param>
        /// <param name="corpsecret">凭证密钥</param>
        /// <returns></returns>
        public static string GetToken(string corpid, string corpsecret)
        {
            Dictionary<string, string> corporation = new()
            {
                {"corpid",corpid },
                {"corpsecret",corpsecret},
            };

            var token = new RestClientWrapper("https://qyapi.weixin.qq.com/cgi-bin/gettoken")
                  .GetRequest<WxTokenModel>(queryParams: corporation);

            if (token == null)
            {
                return string.Empty;
            }

            return token.access_token;
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="token">token</param>
        /// <param name="body">发生内容</param>
        /// <returns></returns>
        public static WxSendModel SendMessage<T>(string token, T body)
        {
            var result = new RestClientWrapper($"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}")
                   .PostRequest<WxSendModel>(body: body);
            return result;
        }

        /// <summary>
        /// 生成markdown消息[可直接传递内容,该方法非必要,只是规范统一的消息模板]
        /// </summary>
        /// <param name="level">推送等级</param>
        /// <param name="orderNo">工单号</param>
        /// <param name="process">工序</param>
        /// <param name="station">工位</param>
        /// <param name="initiator">发起人</param>
        /// <param name="reason">异常原因</param>
        /// <returns></returns>
        public static string CreateMarkDownMsg(string level, string orderNo, string process, string station, string initiator, string reason) =>
                   $"#### <font color=\"info\">蓝鲸提醒</font>\n" + // 优化标题，设置为蓝色
                   $"<font color=\"comment\">推送等级：</font><font color=\"warning\">{level}</font>\n" + // 推送等级为橙色
                   $"<font color=\"comment\">工单号：</font>{orderNo}\n" + // 灰色注释字体
                   $"<font color=\"comment\">工序：</font>{process}\n" +
                   $"<font color=\"comment\">工位：</font>{station}\n" +
                   $"<font color=\"comment\">发起人：</font>{initiator}\n" +
                   $"<font color=\"comment\">发送时间：</font>{DateTime.Now:yyyy年MM月dd日 HH时mm分ss秒}\n" +
                   $"<font color=\"comment\">异常原因：</font><font color=\"red\">{reason}</font>"; // 异常原因高亮为红色
    }
}